<?php
namespace App\Http\Services;
use Illuminate\Support\Facades\Storage;

class FileUtilsServer
{
    /**
     * 上传文件
     * @param $request
     * @return array|string
     */
    public function setFileupload ($request,$id,$folderName) {

        $file = $request->file('file');

        if ($file->isValid()) {
            $originalName = $file->getClientOriginalName(); // 文件原名
            $ext = $file->getClientOriginalExtension();   // 扩展名

            // 上传文件
            $filename = date('Ymd');
            $checkFile = 'public/' . $id .'/' . $filename . '/' . $folderName;
            $newPath1 = 'app/'.$checkFile;

            // 使用我们新建的uploads本地存储空间（目录）
            $res1 = storage_path($newPath1);
            if (!is_dir($res1)) {
                mkdir($res1, 0777, true);
                chmod($res1, 0777);
            }
            $path = $file->store($checkFile);
            $serverKbn = config('businessVolume.SERVER_KBN');
            if ($serverKbn !== "0") {
                $need = '/' . $id .'/' ;
                $fileStore = strstr ($path,$need);
                $awss3 = new AwsS3FilServer();
                $res = $awss3->s3pubFile($fileStore, $res1.'/'.basename($path));
                if ($res) {
                    unlink($res1.'/'.basename($path));
                    exec("rm -rf " . $res1.'/'.basename($path));
                }
            }

            $need = '/' . $id .'/' ;
            $savePath = strstr ($path,$need);

            $data = [];
            $data['clientOriginalName'] = $originalName;
            $data['ext'] = $ext;
            $data['realPath'] = $savePath;
            return $data;
        } else {
            return 'File not verified';
        }
    }

    public function getFilePath () {
    }
}
